/**
 * Author: Ravi Tamada
 * URL: www.androidhive.info
 * twitter: http://twitter.com/ravitamada
 * */
package com.softjourn.punchcard.activity;

import org.springframework.util.StringUtils;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.softjourn.punchcard.R;
import com.softjourn.punchcard.lib.ApiFunctions;
import com.softjourn.punchcard.lib.NetworkUtil;
import com.softjourn.punchcard.lib.SessionManager;
import com.softjourn.punchcard.model.Client;
import com.softjourn.punchcard.model.ClientResponse;

public class RegisterActivity extends ExitDialogSherlockActivity {
	Button btnRegister;
	Button btnLinkToLogin;
	EditText inputFullName;
	EditText inputEmail;
	EditText inputPassword;
	TextView registerErrorMsg;

	// Session Manager Class
	SessionManager session;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		setContentView(R.layout.activity_register);

		getSupportActionBar().hide();
		// Session Manager
		session = new SessionManager(getApplicationContext());

		// Importing all assets like buttons, text fields
		inputFullName = (EditText) findViewById(R.id.registerName);
		inputEmail = (EditText) findViewById(R.id.registerEmail);
		inputPassword = (EditText) findViewById(R.id.registerPassword);
		btnRegister = (Button) findViewById(R.id.btnRegister);
		btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
		registerErrorMsg = (TextView) findViewById(R.id.register_error);

		// Register Button Click event
		btnRegister.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				String name = inputFullName.getText().toString();
				String email = inputEmail.getText().toString();
				String password = inputPassword.getText().toString();

				if (StringUtils.hasText(name) && StringUtils.hasText(email)
						&& StringUtils.hasText(password)) {

					if (NetworkUtil
							.isConnectivityActive(getApplicationContext())) {
						ApiFunctions userFunction = new ApiFunctions(
								getApplicationContext());
						try {
							ClientResponse punchCardResponse = userFunction
									.registerUser(name, email, password);

							if (punchCardResponse.getStatus().equals("OK")) {
								registerErrorMsg.setText("");
								Client client = punchCardResponse.getClient();

								if (client != null) {
									// user successfully registred
									// Store user details in SQLite Database

									// Clear all previous data in database
									// userFunction.logoutUser(getApplicationContext());

									session.createLoginSession(
											client.getName(), client.getUid());
									// Launch Main Screen
									Intent main = new Intent(
											getApplicationContext(),
											MainActivity.class);
									// Close all views before launching
									// Dashboard
									main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
									startActivity(main);
									// Close Registration Screen
									finish();
								} else {
									// Error in registration
									registerErrorMsg
											.setText(R.string.registration_error);
								}

							} else {
								// Error in registration, empty field
								registerErrorMsg.setText(R.string.registration_error);
							}
						} catch (Exception e) {
							showAlertDialog(
									getApplicationContext(),
									getResources().getString(
											R.string.server_error_title),
									getResources().getString(
											R.string.server_error_mess), false);

						}
					} else {
						// Error no internet connection
						showAlertDialog(
								getApplicationContext(),
								getResources().getString(
										R.string.no_internet_connection_title),
								getResources().getString(
										R.string.no_internet_connection_mess),
								false);
					}
				} else {
					// Error in registration
					registerErrorMsg.setText(getBaseContext().getText(
							R.string.empty_fields_error));
				}

			}
		});

		// Link to Login Screen
		btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(),
						LoginActivity.class);
				startActivity(i);
				// Close Registration View
				finish();
			}
		});
	}
}
